package com.imi.threads;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.util.Log;

public class SWThreadExecutor  extends ThreadPoolExecutor
{

	private static int corePoolSize = 2;
	private static int maximumPoolSize =5;
	private static long keepAliveTime = 7;
	private static TimeUnit unit = TimeUnit.SECONDS;
	private volatile static LinkedBlockingQueue<Runnable> queue  = new LinkedBlockingQueue<Runnable>();
	
	private final String TAG = "SWThreadExecutor";
	private final ThreadLocal<String> name = new ThreadLocal<String>();
	/**
	 */
			
	public SWThreadExecutor()
	{
		super(corePoolSize,maximumPoolSize,keepAliveTime,unit,queue,new ThreadPoolExecutor.AbortPolicy());
	
	}	
		
		protected void beforeExecute(Thread t, Runnable r)
		{
			super.beforeExecute(t, r);
			Log.d(TAG,"Starting execution of "+r.toString()+" by "+Thread.currentThread().getName());
			
		}
		
		protected void terminated(){
			Log.d(TAG, "Threadpool is terminated");
		}
		
		protected void afterExecute(Runnable r, Throwable t1)
		{
			try{
				if(t1==null)
				Log.d(TAG,"Execution over with out any exception by "+Thread.currentThread().getName());
				else{
					Log.e(TAG,"Execution incomplete due to Exception by "+Thread.currentThread().getName());	
				}
			}
			finally{
				super.afterExecute(r, t1);
			}
		}
		
			
		
	}
	

	
	


